<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="员工查询"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="find">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="username" label="员工姓名" width="120" sortable>
			</el-table-column>
      <el-table-column prop="phone" label="员工电话" width="120" sortable>
			</el-table-column>
			<el-table-column prop="email" label="邮件" width="100" sortable>
			</el-table-column>
			<el-table-column prop="salt" label="盐值" width="120" sortable>
			</el-table-column>
			<el-table-column prop="password" width="200" label="密码" min-width="180" sortable>
			</el-table-column>
      <el-table-column prop="age" width="100" label="年龄" min-width="100" sortable>
      </el-table-column>
      <el-table-column prop="state" label="员工状态" width="100" :formatter="formatSex" sortable>
        <template scope="scope">
          <span style="color: #13ce66" v-if="scope.row.state==1">工作中</span>
          <span style="color: #1F2D3D" v-else-if="scope.row.state==0">休息中</span>
          <span v-else>带薪休假</span>
        </template>
      </el-table-column>
      <el-table-column prop="department.name" width="150" label="员工所属部门" min-width="180" sortable>
      </el-table-column>
      <el-table-column prop="logininfo.username" width="120" label="登录信息" min-width="180" sortable>
      </el-table-column>
      <el-table-column prop="shop_id" label="管理员" width="100" :formatter="formatSex" sortable>
        <template scope="scope">
          <span style="color: blue" v-if="scope.row.shop_id==null">平台管理员</span>
          <span style="color: #ff4d51" v-else-if="scope.row.shop_id!=null">店铺管理员</span>
          <span v-else>未知</span>
        </template>
      </el-table-column>
<!--      <el-table-column prop="shop.name" width="120" label="店铺" min-width="180" sortable>-->
<!--      </el-table-column>-->

			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :current-page="currentPage" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="员工姓名" prop="username">
					<el-input v-model="editForm.username" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="员工电话" prop="phone">
          <el-input v-model="editForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工邮件" prop="email">
          <el-input v-model="editForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工密码" prop="password">
          <el-input v-model="editForm.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工年龄" prop="age">
          <el-input v-model="editForm.age" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工状态" prop="state">
          <el-input v-model="editForm.state" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="选择部门">
          <el-select v-model="editForm.department" value-key="id" placeholder="请选择">
            <el-option
                v-for="item in dapt"
                :key="item.id"
                :label="item.name"
                :value="item">
              <!--
              value代表要传给后端的对象
              label代表页面显示的数据
              -->
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.id }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="登录信息">
          <el-select v-model="editForm.logininfo" value-key="id" placeholder="请选择">
            <el-option
                v-for="item in logininfo"
                :key="item.id"
                :label="item.username"
                :value="item">
              <!--
              value代表要传给后端的对象
              label代表页面显示的数据
              -->
              <span style="float: left">{{ item.username }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.id }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="店铺" prop="shop_id">
          <el-input v-model="editForm.shop_id" auto-complete="off"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getEmployeeListPage, removeEmployee, batchRemoveEmployee, editEmployee, addEmployee } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
          keyword: ''
				},
				employees: [],
				total: 0,
				pageSize: 5,
        currentPage:1,
				listLoading: false,
				sels: [],//列表选中列
        title:null,
        dapt:[],
        logininfo:[],
        roles:[],

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
          username: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//编辑界面数据
				editForm: {
          id:null,
          username:'',
          phone:'',
          email:'',
          salt:'',
          password:'',
          age:null,
          state:null,
          department_id:null,
          logininfo_id:null,
          shop_id:null,
          department:null,
          shop:null,
          logininfo:null,
          roles:null
				},


			}
		},
		methods: {
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getEmployees();
			},
			//获取员工列表
			getEmployees() {
				let para = {
					pageSize: this.pageSize,
          currentPage: this.currentPage,
          // 模糊查询
          keyword: this.filters.keyword,
				};
				this.listLoading = true;
				// 发送axios请求获取员工数据
        this.$http.post("/employee",para)
            .then(result=>{
              // 关闭忙等
              this.listLoading = false;
              // 将员工数据赋值给页面
              this.employees = result.data.rows;
              // 赋值总条数
              this.total = result.data.total;
              console.log(result.data.shop)
            }).catch(result=>{

        })

        this.listLoading = true;
        // 发送axios请求获取员工数据
        this.$http.get("/role")
            .then(result=>{
              // 关闭忙等
              this.listLoading = false;
              // 将员工数据赋值给页面
              this.roles = result.data;
            }).catch(result=>{

        })

				// //NProgress.start();
				// getEmployeeListPage(para).then((res) => {
				// 	this.total = res.data.total;
				// 	this.employees = res.data.employees;
				// 	this.listLoading = false;
				// 	//NProgress.done();
				// });
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
          // 发起删除请求
          this.$http.delete("/employee/"+row.id).then(result=>{
            // 关闭忙等
            this.listLoading = false;
            // 将页面改为第一页
            this.currentPage = 1;
            // 刷新页面
            this.getEmployees()
            // 判断是否为true
            if(result.data.success){
              this.$message({
                message: result.data.message,
                type: 'success'})
            }else {
              this.$message({
                message: result.data.message,
                type: 'success'})
            }
          }).catch(result=>{
            this.$message({
              message: "系统繁忙",
              type: 'error'})
          })
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
			  this.title = "修改",
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
			  this.title = "新增"
				this.editFormVisible = true;
				this.editForm = {
          id:null,
          username:'',
          phone:'',
          email:'',
          salt:'',
          password:'',
          age:null,
          state:null,
          department_id:null,
          logininfo_id:null,
          shop_id:null,
          department:null,
          shop:null,
          logininfo:null,
				};
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
              // 发起axios请求
              this.$http.put("/employee",para)
                  .then(result=>{
                    // 关闭按钮忙等
                    this.editLoading = false;
                    // 关闭模态框
                    this.editFormVisible = false;
                    // 修改页码为第一页
                    this.currentPage = 1;
                    if (result.data.success){
                      this.$message({
                        message: result.data.message,
                        type: 'success'
                      });
                    }else {
                      this.$message({
                        message: result.data.message,
                        type: 'success'
                      });
                    }
                    // 调用查询方法
                    this.getEmployees()
                  }).catch(result=>{
                // 关闭忙等
                this.editLoading = false;
                // 修改页码为第一页
                this.currentPage = 1;
                this.$message({
                  message: "系统繁忙",
                  type: 'error'
                });
              })


						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					// 发起请求
          this.$http.patch("/employee",ids).then(result=>{
            // 关闭忙等
            this.listLoading = false;
            // 修改页码为第一页
            this.currentPage = 1;
            if (result.data.success){
              this.$message({
                message: result.data.message,
                type: 'success'
              });
            }else {
              this.$message({
                message: result.data.message,
                type: 'success'
              });
            }
            // 调用查询方法
            this.getEmployees()
          }).catch(result=>{
            // 关闭忙等
            this.listLoading = false;
            // 修改页码为第一页
            this.currentPage = 1;
            this.$message({
              message: "系统繁忙",
              type: 'error'
            });
          })
        })
            .catch(() => {

				});
			},
      find(){
			  this.currentPage = 1;
			  this.getEmployees()
      },
      // 查询所有部门
      dapts(){
        this.$http.get("/dept")
            .then(result=>{
              this.dapt = result.data
            }).catch(result=>{

        })
      },
      // 查询所有登录数据
      getLogininfo(){
        this.$http.get("/logininfo")
            .then(result=>{
              this.logininfo = result.data
              console.log("-----")
              console.log(result.data)
            }).catch(result=>{

        })
      },
		},
		mounted() {
			this.getEmployees();
			// 调用查询所有部门
      this.dapts();
      this.getLogininfo();
		}
	}

</script>

<style scoped>

</style>